#!/usr/bin/perl
#
# usage:
#    runTopReader.pl USER1 192.168.60.52 8472

$HOME_DIR = "/home/cloudcom008/". shift;

require "$HOME_DIR/config.ph";

$myAddr = shift;
$myPort = shift;

if($myPort <= 0 || $myAddr eq ''){
      print("error.\nusage: runTopReader.pl USER1 192.168.60.52 8472\n");
      exit(0);
}

$cmd = "killall topReader.pl;./cgi-bin/topReader.pl";

@nodes = &getNodeList();


$prevHost = '';
my(@targetNodeList);
foreach $node (@nodes){
      $uri = &node2uri($node);
      next unless($uri =~ m|http://([^:/]+):?\d*/|);
      $host = $1;
      if($prevHost ne $host){
       if($#targetNodeList >= 0){
        `ssh $prevHost "$cmd $myAddr $myPort @targetNodeList"`;
        #print("ssh $prevHost \"$cmd $myAddr $myPort @targetNodeList\"\n");
       }
       $prevHost = $host;
       undef @targetNodeList;
      }
      push(@targetNodeList, $node);
}

if($#targetNodeList >= 0){
      `ssh $prevHost "$cmd $myAddr $myPort @targetNodeList"`;
      #print("ssh $prevHost \"$cmd $myAddr $myPort @targetNodeList\"\n");
}
